/*
 * @(#)AbstractRule.java  2.0 11/01/11
 */
package com.hidromedsa.measure.entities;

import com.hidromedsa.measure.controller.util.RuleExecutor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * <p>Class that represents an abstract rule</p>
 *
 * @version 2.0, 11/01/11
 * @since JDK1.0
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "rule")
public class AbstractRule implements Serializable, RuleExecutor, Comparable<AbstractRule> {

    private FieldMedicion fieldIni;
    private String resultName;
    private RuleTypesEnum rules;
    private FieldMedicion resultField;
    private int position;
    private int startPositionIni;
    private int finalPostionIni;
    private int spaceNumber;
    private FieldMedicion fieldEnd;
    private int startPositionEnd;
    private int finalPostionEnd;

    public AbstractRule() {
    }

    public AbstractRule(FieldMedicion fieldIni, String resultName, RuleTypesEnum rules, int position, int startPositionIni, int finalPostionIni, int spaceNumber, FieldMedicion fieldEnd, int startPositionEnd, int finalPostionEnd) {
        this.fieldIni = fieldIni;
        this.resultName = resultName;
        this.rules = rules;
        this.position = position;
        this.startPositionIni = startPositionIni;
        this.finalPostionIni = finalPostionIni;
        this.spaceNumber = spaceNumber;
        this.fieldEnd = fieldEnd;
        this.startPositionEnd = startPositionEnd;
        this.finalPostionEnd = finalPostionEnd;
    }

    public FieldMedicion getFieldIni() {
        return fieldIni;
    }

    public void setFieldIni(FieldMedicion fieldIni) {
        this.fieldIni = fieldIni;
    }

    public String getResultName() {
        return resultName;
    }

    public FieldMedicion getResultField() {
        return resultField;
    }

    public void setResultField(FieldMedicion resultField) {
        this.resultField = resultField;
    }

    public void setResultName(String resultName) {
        this.resultName = resultName;
    }

    public RuleTypesEnum getRules() {
        return rules;
    }

    public void setRules(RuleTypesEnum rules) {
        this.rules = rules;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public FieldMedicion getFieldEnd() {
        return fieldEnd;
    }

    public void setFieldEnd(FieldMedicion fieldEnd) {
        this.fieldEnd = fieldEnd;
    }

    public int getFinalPostionEnd() {
        return finalPostionEnd;
    }

    public void setFinalPostionEnd(int finalPostionEnd) {
        this.finalPostionEnd = finalPostionEnd;
    }

    public int getFinalPostionIni() {
        return finalPostionIni;
    }

    public void setFinalPostionIni(int finalPostionIni) {
        this.finalPostionIni = finalPostionIni;
    }

    public int getSpaceNumber() {
        return spaceNumber;
    }

    public void setSpaceNumber(int spaceNumber) {
        this.spaceNumber = spaceNumber;
    }

    public int getStartPositionEnd() {
        return startPositionEnd;
    }

    public void setStartPositionEnd(int startPositionEnd) {
        this.startPositionEnd = startPositionEnd;
    }

    public int getStartPositionIni() {
        return startPositionIni;
    }

    public void setStartPositionIni(int startPositionIni) {
        this.startPositionIni = startPositionIni;
    }

    public void executeRules() {

        FieldMedicion fieldResultLocal = new FieldMedicion();
        fieldResultLocal.setName(this.getResultName());
        List<String> values = new ArrayList<String>();
        switch (rules) {
            case CONCAT:

                String space = "";
                for (int i = 0; i < this.getSpaceNumber(); i++) {
                    space += " ";
                }
                for (int i = 0; i < this.getFieldIni().getValues().size(); i++) {

                    String line = "";
                    String dataIni = this.getFieldIni().getValues().get(i);
                    String dataFin = this.getFieldEnd().getValues().get(i);
                    System.out.println(dataIni + "," + dataFin);
                    if (this.getFinalPostionIni() == 0) {
                        line += dataIni;
                    } else {
                        try {
                            line += dataIni.substring(this.getStartPositionIni(), this.getFinalPostionIni());
                        } catch (Exception e) {
                            line += dataIni;
                        }
                    }
                    line += space;
                    if (this.getFinalPostionEnd() == 0) {
                        line += dataFin;
                    } else {
                        try {
                            line += dataFin.substring(this.getStartPositionEnd(), this.getFinalPostionEnd());
                        } catch (Exception e) {
                            line += dataIni;
                        }
                    }
                    System.out.println(line);
                    values.add(line);

                }
                fieldResultLocal.setValues(values);
                fieldResultLocal.setPosition(this.getPosition());
                fieldResultLocal.setType(FieldTypesEnum.VARCHAR);
                this.setResultField(fieldResultLocal);
                break;
            case DUPLICITY:
                for (int i = 0; i < this.getFieldIni().getValues().size(); i++) {
                    String dataIni = this.getFieldIni().getValues().get(i);
                    String line = dataIni;
                    values.add(line);
                }
                fieldResultLocal.setValues(values);
                fieldResultLocal.setPosition(this.getPosition());
                fieldResultLocal.setType(FieldTypesEnum.VARCHAR);
                this.setResultField(fieldResultLocal);
                break;
            case ANY:
                this.setResultField(this.getFieldIni());
                break;
            case SPLIT:
                for (int i = 0; i < this.getFieldIni().getValues().size(); i++) {

                    String line = "";
                    String dataIni = "";
                    dataIni = this.getFieldIni().getValues().get(i);
                    
                    System.out.println(dataIni + ":" +this.getStartPositionIni() + "to" +this.getFinalPostionIni());
                    try {
                        line = dataIni.substring(this.getStartPositionIni(), this.getFinalPostionIni());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    values.add(line);
                    System.out.println(line);
                }
                fieldResultLocal.setValues(values);
                fieldResultLocal.setPosition(this.getPosition());
                fieldResultLocal.setType(FieldTypesEnum.VARCHAR);
                this.setResultField(fieldResultLocal);

                break;
        }
    }

    public int compareTo(AbstractRule o) {
        return o.getResultName().equals(this.getResultName()) ? 0 : 1;
    }
}
